package gui.admin;

import java.sql.SQLException;

import objects.Student;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class AdminNewDriverPanelImpl extends AbstractAdminPanelImpl implements AdminNewDriverPanel {
	private Label driverNameLbl;
	private Label instructorNameLbl;
	private Label schoolsAddressLbl;
	private Label driversAddressLbl;
	private Label unitNumberLbl;
	private Button cancelBtn;
	private Button saveBtn;
	private Text firstName;
	private Text instructorsName;
	private Text schoolAddress;
	private Text driverAddress;
	private Text unitNumber;
	private Label instructionLbl;
	
	private AbstractAdminPanelImpl caller;
	private Text lastName;
	private Label lastNameLbl;
	private Label firstNameLbl;
	
	public AdminNewDriverPanelImpl(Display display, Shell shell) {
		super(display, shell);
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	public void open() {
		// Remove at compile time
//		Shell shell = new Shell();
		// **********************
		
		shell.setSize(500, 400);
		shell.setText("New Driver");
		
		driverNameLbl = new Label(shell, SWT.NONE);
		driverNameLbl.setBounds(10, 60, 124, 22);
		driverNameLbl.setText("Driver's Name:");
		driverNameLbl.setFont(new Font(display, "Arial", 14, SWT.NONE));
		
		instructorNameLbl = new Label(shell, SWT.NONE);
		instructorNameLbl.setBounds(10, 107, 139, 22);
		instructorNameLbl.setText("Instructor Name:");
		instructorNameLbl.setFont(new Font(display, "Arial", 14, SWT.NONE));
		
		schoolsAddressLbl = new Label(shell, SWT.NONE);
		schoolsAddressLbl.setBounds(10, 135, 218, 22);
		schoolsAddressLbl.setText("School's Mailing Address:");
		schoolsAddressLbl.setFont(new Font(display, "Arial", 14, SWT.NONE));
		
		driversAddressLbl = new Label(shell, SWT.NONE);
		driversAddressLbl.setBounds(10, 211, 209, 22);
		driversAddressLbl.setText("Driver's Mailing Address:");
		driversAddressLbl.setFont(new Font(display, "Arial", 14, SWT.NONE));
		
		unitNumberLbl = new Label(shell, SWT.NONE);
		unitNumberLbl.setBounds(10, 284, 155, 22);
		unitNumberLbl.setText("OBU Unit Number:");
		unitNumberLbl.setFont(new Font(display, "Arial", 14, SWT.NONE));
		
		cancelBtn = new Button(shell, SWT.NONE);
		cancelBtn.setBounds(399, 311, 75, 25);
		cancelBtn.setText("Cancel");
		cancelBtn.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				getCaller().setEnabled(true);
				shell.dispose();
			}
		});
		
		saveBtn = new Button(shell, SWT.NONE);
		saveBtn.setBounds(318, 311, 75, 25);
		saveBtn.setText("Save");
		saveBtn.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				Student student = new Student();
				student.setAddress(getDriverAddress());
				student.setName(getDriversName());
				student.setInstrID(instructor.getID());
				student.setSchoolID(school.getID());
				
				Student temp = new Student(1);
				try {
					database.getStudent(temp);
				} catch (SQLException e1) {
					System.out.println("Unable to get student");
					e1.printStackTrace();
				}
				
				student.setFingerPrint(temp.getFingerPrint());
				
				try {
					database.addStudent(student);
				} catch (SQLException e) {
					System.out.println("Unable to add student");
					e.printStackTrace();
				}
				
				getCaller().setEnabled(true);
				shell.dispose();
			}
		});
		
		firstName = new Text(shell, SWT.BORDER);
		firstName.setBounds(140, 60, 159, 22);
		
		instructorsName = new Text(shell, SWT.BORDER);
		instructorsName.setBounds(155, 108, 319, 21);
		
		schoolAddress = new Text(shell, SWT.BORDER);
		schoolAddress.setBounds(234, 138, 240, 67);
		
		driverAddress = new Text(shell, SWT.BORDER);
		driverAddress.setBounds(234, 211, 240, 67);
		
		unitNumber = new Text(shell, SWT.BORDER);
		unitNumber.setBounds(171, 284, 303, 21);
		
		instructionLbl = new Label(shell, SWT.WRAP);
		instructionLbl.setBounds(10, 10, 464, 44);
		instructionLbl.setText("Please enter the required information and press the save button when finished.");
		instructionLbl.setFont(new Font(display, "Arial", 14, SWT.ITALIC));
		
		lastName = new Text(shell, SWT.BORDER);
		lastName.setBounds(305, 60, 169, 22);
		
		firstNameLbl = new Label(shell, SWT.NONE);
		firstNameLbl.setBounds(191, 87, 57, 15);
		firstNameLbl.setText("First Name");
		
		lastNameLbl = new Label(shell, SWT.NONE);
		lastNameLbl.setBounds(369, 88, 57, 15);
		lastNameLbl.setText("Last Name");

		// fill current labels
		setInstructorsName(instructor.getName());
		setSchoolAddress(school.getAddress());
		
		menuBar.setVisible(false);
		
		shell.open();
		shell.layout();
	}

	public String getDriversName() {
		return this.firstName.getText() + ", " + this.lastName.getText();
	}

	public String getInstructorsName() {
		return this.instructorsName.getText();
	}

	public void setInstructorsName(String name) {
		instructorsName.setText(name);
		instructorsName.update();
	}
	
	public String getDriverAddress() {
		return driverAddress.getText();
	}

	public String getSchoolAddress() {
		return schoolAddress.getText();
	}

	public void setSchoolAddress(String address) {
		schoolAddress.setText(address);
		schoolAddress.update();
	}

	public Integer getUnitNumber() {
		return Integer.parseInt(unitNumber.getText());
	}
	
	public void setCaller(AbstractAdminPanelImpl caller) {
		this.caller = caller;
	}
	
	public AbstractAdminPanelImpl getCaller() {
		return this.caller;
	}
}
